package com.king01.controller;

import com.king01.entity.CountVo;
import com.king01.service.StatisticsService;
import com.king01.vo.DateVo;
import com.king01.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author ：lqs
 * @date ：Created in 2023/10/9 17:24
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/sta")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @RequestMapping("/trade")
    public Result trade(@RequestBody List<Date> date){
       // 营业额
        Integer yy = 0;
        Integer cb = 0;
        DateVo dateVo1 = new DateVo();
        if (date.isEmpty()){

        }else {
            dateVo1.setDate1(date.get(0));
            dateVo1.setDate2(date.get(1));
        }


        Result result = statisticsService.trade(dateVo1);
        List<Integer> data = (List<Integer>)result.getData();
        for (Integer datum1 : data) {
            if (datum1!=null) {
                yy = yy + datum1;
            }
        };

        Result result2 = statisticsService.trade2(dateVo1);
        List<Integer> data2 = (List<Integer>)result2.getData();
        for (Integer datum2 : data2) {
            if(datum2!=null) {
                cb = cb + datum2;
            }
        }
        List<Map<String,Object>> mapList = new ArrayList<>();

        Map<String, Object> objectObjectMap1 = new LinkedHashMap<>();
        objectObjectMap1.put("value",yy);
        objectObjectMap1.put("name","营业额");

        Map<String, Object> objectObjectMap2 = new  LinkedHashMap<>();
        objectObjectMap2.put("value",cb);
        objectObjectMap2.put("name","成本额");

        mapList.add(objectObjectMap1);
        mapList.add(objectObjectMap2);



        return new Result(200,"",mapList) ;
    }

    @RequestMapping("/byBranch")
    public Result byBranch(@RequestBody List<Date> date){
        DateVo dateVo1 = new DateVo();
        if (date.isEmpty()){

        }else {
            dateVo1.setDate1(date.get(0));
            dateVo1.setDate2(date.get(1));
        }
        Result result = statisticsService.byBranch(dateVo1);
        return result;
    }
    @RequestMapping("/byGoods")
    public Result byGoods(@RequestBody List<Date> date){
        DateVo dateVo1 = new DateVo();
        if (date.isEmpty()){

        }else {
            dateVo1.setDate1(date.get(0));
            dateVo1.setDate2(date.get(1));
        }
        Result result = statisticsService.byGoods(dateVo1);

        List<List<Object>> list = new ArrayList<>();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();

        for (CountVo datum : (List<CountVo>)result.getData()) {
           list1.add(datum.getGoodsname());
           list2.add(datum.getCount());
        }
        list.add(list1);
        list.add(list2);

        return new Result(200,"",list);
    }
}

